# 编写一个方法 接受图像和阈值 用于S通道并返回二值输出

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import cv2

# 传入图象
image = mpimg.imread('test.jpg') 

def hls_select(img, thresh=(0, 255)):
    # 1) 获得HLS色彩空间
    hls = cv2.cvtColor(image, cv2.COLOR_RGB2HLS)

    # 2) 获取S通道空间
    S = hls[:,:,2]

    # 3) 返回二值化后的图片
    binary_output = np.zeros_like(S)
    binary_output[(S > thresh[0]) & (S <= thresh[1])] = 1
    return binary_output
    
# 执行函数
hls_binary = hls_select(image, thresh=(0, 255))

# 可视化输出
f, (ax1, ax2) = plt.subplots(1, 2, figsize=(24, 9))
f.tight_layout()
ax1.imshow(image)
ax1.set_title('Original Image', fontsize=50)
ax2.imshow(hls_binary, cmap='gray')
ax2.set_title('Thresholded S', fontsize=50)
plt.subplots_adjust(left=0., right=1, top=0.9, bottom=0.)
plt.show()